var dialogWidth=1100;
var dialogHeight=800;
var produceController={
    saveurl:"",
	init:function(){
        /**
         * 显示左面导航
         */
        showLeftStyle("product");
        produceController.initTable();
        produceController.setListeners();
	},
	setListeners:function(){
        $("#addrecord").click(function(){
            produceController.addrecord();
        });

        $("#updaterecord").click(function(){
            produceController.updaterecord();
        });

        //保存
        $("#btnsubmit").click(function(){
            produceController.save();
        });

        //删除
        $("#deleterecord").click(function(){
            produceController.deleterecord();
        });

        //查询
        $("#searchtable").click(function(){
            produceController.search();
        });

	},
    initTable:function(){
        /**
         * 表格
         */
        $('#dg').datagrid( {
            width: clientWidth-200,
            height: clientHeight-180,
            rownumbers : true,
            singleSelect : true,
            pagination : true,
            pageSize : 20,
            fitColumns: true,
            url : basePath+'/product/listPage',
        });
	},
	search:function(){
        var postData = {
            params : {}
        };
        var name=$('#tb #name').val();
        if(name){
            postData.params.name = "%"+name.trim()+"%";
        }
        $("#dg").datagrid("options").queryParams = postData;
        $("#dg").datagrid("reload");
	},
    addrecord:function(){
        produceController.saveurl=basePath+'/product/create';
        $('#dd').dialog({
            iconCls:'',
            title: '新增产品',
            width: dialogWidth,
            height: dialogHeight,
            closed: false,
            cache: false,
            resizable:true,
            href: basePath+'/product/showaddpage',
            modal: true,
            onLoad:function(){
                $("#typeId").chosen();
            }
        });
        //$('#dd').dialog('refresh', 'addpwk.jsp');
    },
    updaterecord:function(){
        var row = $('#dg').datagrid('getSelected');
        if(row==null){
            $.messager.alert('提示信息','请选择一条记录!','info');
            return;
        }
        pkeyId = row.id;
        tableName = "Product";
        produceController.saveurl=basePath+'/product/update';
        if(row.isrecom == "true" || row.isrecom){
            row.isrecom=1;
        }else{
            row.isrecom=0;
        }
        $('#dd').dialog({
            iconCls:'',
            title: '编辑产品',
            width: dialogWidth,
            height: dialogHeight,
            closed: false,
            cache: false,
            resizable:true,
            href: basePath+'/product/showaddpage',
            modal: true,
            onLoad:function(){
                $('#form12').form('load', row);
                $("#typeId").chosen();
            }
        });
        //$('#dd').dialog('refresh', 'addpwk.jsp');
    },
    deleterecord:function(){
        var row = $('#dg').datagrid('getSelected');
        if(row==null){
            $.messager.alert('提示信息','请选择一条记录!','info');
            return;
        }
        $.messager.confirm('删除记录', '确认删除吗?', function(r){
            if (r){
                showProcess(true, '温馨提示', '正在提交数据...');
                $.post(basePath+'/product/delete',{id:row.id},function(json){
                    showProcess(false);
                    if(json=="删除成功"){
                        $.messager.alert('提示信息','删除成功!','info',function () {
                            $('#dg').datagrid('reload');
                        });
                    }else{
                        $.messager.alert('提示信息','删除失败!','error');
                    }
                });
            }
        });
    },
    changestatus:function(index, status, tipname){
        // var row = $('#dg').datagrid('getSelected');
        var rows = $('#dg').datagrid('getRows');
        var row = rows[index];
        if(row==null){
            $.messager.alert('提示信息','请选择一条记录!','info');
            return;
        }
        $.messager.confirm('提示', '确认'+tipname+'产品【'+row.name+'】吗?', function(r){
            if (r){
                showProcess(true, '温馨提示', '正在提交数据...');
                $.post(basePath+'/product/changestatus',{id:row.id,status:status},function(json){
                    showProcess(false);
                    if(json=="修改成功"){
                        $.messager.alert('提示信息',tipname+'成功!','info',function () {
                            $('#dg').datagrid('reload');
                        });
                    }else{
                        $.messager.alert('提示信息',tipname+'失败!','error');
                    }
                });
            }
        });
    },
    save:function(){
        $('#form12').form('submit', {
            url : produceController.saveurl,
            onSubmit : function() {
                var typeId = $('#form12 #typeId').val();
                if(!typeId){
                    alert("请选择产品类型");
                    return false;
                }

                //进行表单验证
                //如果返回false阻止提交
                var flag = $(this).form('validate');
                if (flag) {
                    showProcess(true, '温馨提示', '正在提交数据...');
                }
                return flag
            },
            success : function(data) {
                showProcess(false);
                if (data=='录入成功'||data=='修改成功') {
                    $.messager.alert('提示信息','提交成功！','info');
                    $('#dg').datagrid('reload');
                    $('#dd').dialog('close');
                }  else if (data=='1') {
                    $.messager.alert('提示信息','您输入的产品编号已存在，请重新输入！','error');
                }  else if (data=='2') {
                    $.messager.alert('提示信息','您输入的产品名称已存在，请重新输入！','error');
                }  else{
                    $.messager.alert('提示信息','提交失败！','error');
                }
            },
            error:function (data){
                $.messager.alert('提示信息','提交失败！','error');
            }
        });
    }

};
$(function(){
    produceController.init();
    //chosen select
    // $(".chzn-select-deselect").chosen({allow_single_deselect:true});
});

